<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{Order, Quotes};
use think\Facade\Config as Configs;
use think\Db;
use app\facade\Redis;
use think\facade\Debug;


class CreatOrder extends Command
{
    protected function configure()
    {
        $this->setName('CreatOrder')->setDescription('生成假订单');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/JTE/think MatchOrder'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        (new Quotes())->where(['istrans' => 1, 'ishow' => 1])->chunk(1, function ($list) {
            foreach ($list as $lists) {
                //先查询买单市价订单是否存在
                $buyorder = (new Order())->where(['type' => 1, 'prmoney' => 2, 'coin' => $lists['coin'], 'mart' => $lists['mart'], 'status' => 1])->count();
                //查询卖单市价订单是否存在
                $sellorder = (new Order())->where(['type' => 2, 'prmoney' => 2, 'coin' => $lists['coin'], 'mart' => $lists['mart'], 'status' => 1])->count();
                Db::startTrans();
                try {
                    if ($buyorder == 0) {
                        (new Order())->save([
                            'type' => 1,
                            'prmoney' => 2,
                            'coin' => $lists['coin'],
                            'mart' => $lists['mart'],
                            'status' => 1,
                            'commtotal' => 10000,
                        ]);
                    }
                    if ($sellorder == 0) {
                        (new Order())->save([
                            'type' => 2,
                            'prmoney' => 2,
                            'coin' => $lists['coin'],
                            'mart' => $lists['mart'],
                            'status' => 1,
                            'commnum' => 10000,
                        ]);
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    dump($e->getMessage());
                }
            }
        });
    }
}
